package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.ContentsMapper;
import top.maof.book.model.Contents;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.ContentsService;
import top.maof.book.utils.Utils;

import java.util.List;

@Service("contentsServiceImpl")
public class ContentsServiceImpl extends ServiceImpl<ContentsMapper, Contents> implements ContentsService {
    @Autowired
    private ContentsMapper contentsMapper;

    @Override
    public PageData<Contents> findPage(PageWrap<Contents> pageWrap) {
        IPage<Contents> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Contents> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(Contents::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getContent() != null) {
            queryWrapper.lambda().eq(Contents::getContent, pageWrap.getModel().getContent());
        }
        if (pageWrap.getModel().getUrl() != null) {
            queryWrapper.lambda().eq(Contents::getUrl, pageWrap.getModel().getUrl());
        }
        if (pageWrap.getModel().getBookId() != null) {
            queryWrapper.lambda().eq(Contents::getBookId, pageWrap.getModel().getBookId());
        }
        if (pageWrap.getModel().getWebId() != null) {
            queryWrapper.lambda().eq(Contents::getWebId, pageWrap.getModel().getWebId());
        }
        if (pageWrap.getModel().getUpdateDate() != null) {
            queryWrapper.lambda().ge(Contents::getUpdateDate, Utils.Date.getStart(pageWrap.getModel().getUpdateDate()));
            queryWrapper.lambda().le(Contents::getUpdateDate, Utils.Date.getEnd(pageWrap.getModel().getUpdateDate()));
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(contentsMapper.selectPage(page, queryWrapper));
    }


    public int add(Contents contents) {
        synchronized (this.getClass()) {
            if (contentsMapper.findByUrl(contents.getUrl()) == null)
                return contentsMapper.insert(contents);
            return 0;
        }
    }

    public Contents find(int id) {
        return contentsMapper.selectByPrimaryKey(id);
    }

    @Override
    public Contents findByUrl(String url) {
        return contentsMapper.findByUrl(url);
    }

    public int update(Contents contents) {
        return contentsMapper.updateByPrimaryKeySelective(contents);
    }

    public int delete(int id) {
        return contentsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Contents> findByBookId(int bookId) {
        return contentsMapper.findByBookId(bookId);
    }

    @Override
    public List<Contents> findByBookIdAndWebId(Integer bookId, Integer webId) {
        return contentsMapper.findByBookIdAndWebId(bookId, webId);
    }

    @Override
    public int updateByPrimaryKeySelective(Contents record) {
        return contentsMapper.updateByPrimaryKeySelective(record);
    }
}
